Skip to content

Combined PR to show ContextVar changes together#154647

Draft
nascheme wants to merge 6 commits into
python:mainfrom
nascheme:ctx_thread_inherit_combined
Draft

Combined PR to show ContextVar changes together#154647
nascheme wants to merge 6 commits into
python:mainfrom
nascheme:ctx_thread_inherit_combined

Conversation

@nascheme

@nascheme nascheme commented Jul 24, 2026

Copy link
Copy Markdown
Member

This is a draft PR and is not meant to be merged as it. It shows the result after merging the following changes:

Related discussion is on Discourse.

Reasoning for each change:

  • ContextVar.thread_inheritable(): this new API is for libraries that want to convert global state to context-local state. It was intended that they should use ContextVar() since it acts like a thread-local and also switches context for asyncio tasks. However, ContextVar() has an issue in that new threads start with empty ContextVar bindings, they don't inherit the binding of the variable from the starting thread. This was an oversight but it has been how it works since contextvars was introduced. So changing it so threads do inherit is an incompatible change (effectively, this is turning the thread_inherit_context flag on by default). The ContextVar.thread_inheritable() API allows libraries to opt-in to what will be future behavior early. Suggest to merge for the 3.15 release.
  • deprecation warning: this warning will help identify code that will be affected by changing the default of thread_inherit_context to true. Ideally this warning should be in place for two releases before we change the default. Suggested to merge for the 3.16 release. Merging for 3.15 might be okay but I fear of people seeing new deprecation warnings that were not present in earlier 3.15 betas.
  • Make decimal.getcontext() return a fresh copy: this is required to avoid a bug if thread_inherit_context is true or if decimal starts using ContextVar.thread_inheritable() for its context. In that case, threads inherit the same mutable decimal.Context instance of the thread that spawned them. That's not thread safe and very likely not desired (e.g. a thread modifying the decimal context could affect the context of other threads). This bug exists in current Python if you explicitly set thread_inherit_context or if you use the free-threaded build, where that's set by default. Ideally this would be merged before 3.15 final since it's a bug.
  • Future PR, use ContextVar.thread_inheritable() for warnings and decimal: this change avoids deprecation warnings from these modules. Suggested to merge in 3.16. This changes decimal context slightly in the default (GIL-enabled) build (new threads start with a copy of the decimal context of the starter thread, rather than a copy of decimal.DefaultContext). IMHO, this is better but it is also an incompatible change.

In a future release (likely 3.18), we turn thread_inherit_context on by default. Then, ContextVar.thread_inheritable() and ContextVar() do the same thing. We can just leave thread_inheritable() as a soft-deprecated alias.

nascheme added 5 commits July 24, 2026 12:09
On GIL-enabled builds, emit a DeprecationWarning when the default
implicitly empty threading.Thread() context causes a context variable
lookup to differ from the context that a future release will inherit by
default.
Context variables created this way will automatically be inherited by
the context for new threads, regardless of the setting of
`thread_inherit_context`.
We need a per-task copy of decimal.Context() so that mutations are
isolated between asyncio tasks and threads using
sys.flags.thread_inherit_context.  This is done by adding a "depth"
counter to PyContext and copying the decimal.Context() instance whenever
it doesn't match the depth of the current PyContext.
@read-the-docs-community

read-the-docs-community Bot commented Jul 24, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33750081 | 📁 Comparing fbddda8 against main (998fc4a)

  🔍 Preview build  

6 files changed · ± 6 modified

± Modified

Change `warnings` and `decimal` to use thread inheritable context vars.
This means new threads will start with bindings for these variables from
the starter thread, rather than having empty bindings (no matter the
value of the thread_inherit_context flag).
@nascheme
nascheme force-pushed the ctx_thread_inherit_combined branch from bdbb491 to fbddda8 Compare July 24, 2026 20:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant